package com.mock.water.core.serialize;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.IntNode;
import com.mock.water.modules.system.role.entity.RoleEntity;
import com.mock.water.modules.system.user.entity.UserEntity;

import java.io.IOException;

/**
 * $$ 自定义序列化器
 * 序列化UserEntity对象时，自定在Role角色上设置roleName
 *
 * @author ifredomvip@gmail.com
 * @version 1.0.0
 * @date 2022/10/17 16:07
 **/
public class DefineMySerialHandle extends StdDeserializer<UserEntity> {

    public DefineMySerialHandle() {
        this(null);
    }

    protected DefineMySerialHandle(Class<?> vc) {
        super(vc);
    }

    @Override
    public UserEntity deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        String phone = node.get("phone").asText();
        String pwd = node.get("password").asText();

        RoleEntity role = new RoleEntity();
        role.setRoleName("freedom");

        return new UserEntity(phone, pwd);
    }
}
